// 2025/4/30
// 走迷宫


// dfs超时
#include <iostream>
#include <vector>
using namespace std;

void dfs(vector<string>& matrix, vector<vector<int>>& time, int x, int y, int pre)
{
    if(time[x][y] == -1 || pre + 1 < time[x][y])
        time[x][y] = pre + 1;
    else
        return;

    matrix[x][y] = '*';
    if(x > 0 && matrix[x - 1][y] == '.')
        dfs(matrix, time, x - 1, y, time[x][y]);
    if(x < matrix.size() - 1 && matrix[x + 1][y] == '.')
        dfs(matrix, time, x + 1, y, time[x][y]);
    if(y > 0 && matrix[x][y - 1] == '.')
        dfs(matrix, time, x, y - 1, time[x][y]);
    if(y < matrix[0].size() - 1 && matrix[x][y + 1] == '.')
        dfs(matrix, time, x, y + 1, time[x][y]);
    matrix[x][y] = '.';
}

int main() {
    int m ,n, x1, y1, x2, y2; 
    cin >> m >> n;
    cin >> x1 >> y1 >> x2 >> y2;
    x1--; y1--; x2--; y2--;
    vector<string> matrix(m);
    for(int i = 0; i < m; i++)
    {
        cin >> matrix[i];
    }
    if(matrix[x2][y2] == '*' || matrix[x1][y1] == '*')
    {
        cout << -1 << endl;
        return 0;
    }
    vector<vector<int>> time(m, vector<int>(n, -1));
    dfs(matrix, time, x2, y2, -1);
    cout << time[x1][y1] << endl;
    return 0;
}
// 64 位输出请用 printf("%lld")